﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace AISFMIOperator
{
    class MedicalService
    {
        public MedicalService()
        { }

        public int MedicalServiceID
        { get; set; }

        public string MedicalServiceName
        { get; set; }

        public float Cost
        { get; set; }

        public void Save()
        {
            SqlCommand command = new SqlCommand("CreateMedicalService");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@service_name", SqlDbType.VarChar, 1000).Value = MedicalServiceName;
            command.Parameters.Add("@cost", SqlDbType.Float).Value = Cost;
            command.Parameters.Add("@service_id", SqlDbType.Int).Direction = ParameterDirection.Output;
            SQLDBLayer.ExecSPNonQuery(command);
            this.MedicalServiceID = (int)command.Parameters["@service_id"].Value;
        }

        public void Update()
        {
            SqlCommand command = new SqlCommand("UpdateMedicalService");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@service_name", SqlDbType.VarChar, 1000).Value = MedicalServiceName;
            command.Parameters.Add("@cost", SqlDbType.Float).Value = Cost;
            command.Parameters.Add("@service_id", SqlDbType.Int).Value = MedicalServiceID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public void Delete()
        {
            SqlCommand command = new SqlCommand("DeleteMedicalService");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@service_id", SqlDbType.Int).Value = MedicalServiceID;
            SQLDBLayer.ExecSPNonQuery(command);
        }

        public static List<MedicalService> GetAllMedicalServices()
        {
            DataTable table = new DataTable();
            List<MedicalService> services = new List<MedicalService>();
            SqlCommand command = new SqlCommand("GetMedicalServices");
            command.CommandType = CommandType.StoredProcedure;

            table = SQLDBLayer.ExecSPTable(command);

            foreach (DataRow row in table.Rows)
            {
                MedicalService service = new MedicalService();
                service.MedicalServiceID = (int)row["service_id"];
                service.MedicalServiceName = (string)row["service_name"];
                service.Cost = float.Parse(row["cost"].ToString());

                services.Add(service);
            }

            return services;
        }
    }
}
